package com.crm.service.saling.impl;

import com.crm.dao.saling.PlaningBackDao;
import com.crm.exception.ServiceException;
import com.crm.pojo.PlaningBack;
import com.crm.service.saling.PlaningBackService;
import com.crm.vo.PlaningBackClientContractUserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by dr5hx on 2018/8/26
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PlaningServiceImpl implements PlaningBackService {
    @Autowired
    private PlaningBackDao planingBackDao;

    /**
     * 获取回款数量
     *
     * @return
     */
    @Override
    public Long getPlaningBackCount() {
        return planingBackDao.getPlaningBackCount();
    }

    /**
     * 分页显示回款数据
     *
     * @param currpage
     * @param rows
     * @return
     * @throws ServiceException
     */
    @Override
    public List<PlaningBackClientContractUserInfoVO> getPlaningBackClientContractUserInfoVOList(int currpage, int rows) throws ServiceException {
        try {
            PageRequest pageRequest = new PageRequest(currpage - 1, rows);
            return planingBackDao.getPlaningBackClientContractUserInfoVOList(pageRequest);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    /**
     * 获取一个计划回款
     *
     * @param planingBack
     * @return
     * @throws ServiceException
     */
    @Override
    public PlaningBack getPlaningBackRecord(Long planingBack) throws ServiceException {

        try {
            return planingBackDao.getPlaningBackRecord(planingBack);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }


    /**
     * 获取对应合同的对应回款计划数据
     *
     * @param ContractId
     * @return
     * @throws ServiceException
     */
    @Override
    public List<PlaningBack> getPlaningBackByContractId(Long ContractId) throws ServiceException {
        try {
            return planingBackDao.getPlaningBackByContractId(new BigDecimal(ContractId.toString()));
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    /**
     * 保存回款计划
     *
     * @param planingBack
     * @return
     * @throws ServiceException
     */
    @Override
    public Boolean savePlaningBackData(PlaningBack planingBack) throws ServiceException {
        try {
            return planingBackDao.save(planingBack) == null ? false : true;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    /**
     * 保存回款计划列表
     *
     * @param list
     * @return
     * @throws ServiceException
     */
    @Override
    public Boolean savePlaningBackDataList(List<PlaningBack> list) throws ServiceException {
        try {
            return planingBackDao.save(list) == null ? false : true;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    /**
     * 统计对应合同的所有计划回款金额
     *
     * @param contractId
     * @return
     */
    @Override
    public BigDecimal countPlaningBackMoneyByContractId(Long contractId) {
        return planingBackDao.countPlaningBackMoneyByContractId(new BigDecimal(contractId.toString()));
    }
}
